package com.zmn.mcc.admin.controller.permit;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.common.constant.RoleDict;
import com.zmn.mcc.model.entity.module.McModule;
import com.zmn.mcc.model.entity.permit.McPermit;
import com.zmn.mcc.model.entity.permit.McPermitQuery;
import com.zmn.mcc.model.entity.role.McRole;
import com.zmn.mcc.model.entity.role.McRoleQuery;
import com.zmn.mcc.model.entity.sys.McSys;
import com.zmn.mcc.services.interfaces.role.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/15 10:50
 */
@Controller
@RequestMapping("/permit/")
public class PermitListController extends PermitBaseController {


    /**
     * 权限列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @ModelAttribute McPermitQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setPermitId(Integer.parseInt(keyword));
            query.setPermitName("");
        } else {
            query.setPermitName(keyword);
        }

        List<McPermit> items = permitService.listPageByQuery(query);

        McModule module = new McModule();
        if(NumberUtil.isNotNullOrZero(query.getModuleId())) {
            module = moduleService.findByKey(query.getModuleId());
            query.setSysId(module.getSysId());
        }
        McSys sys = new McSys();
        if(NumberUtil.isNotNullOrZero(query.getSysId())) {
            sys = sysService.findByKey(query.getSysId());
        }

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("module", module);
        model.addAttribute("sys", sys);

        model.addAttribute("keyword", keyword);

        return "permit/list";
    }

    /**
     * 权限详情
     */
    @RequestMapping("detail")
    public String detail(HttpServletRequest request,
                         @RequestParam(value = "moduleId", defaultValue = "0") Integer moduleId,
                         @RequestParam(value = "permitId", defaultValue = "0") Integer permitId,
                         Model model) {

        McPermit obj = !NumberUtil.isNullOrZero(permitId) ? permitService.findByKey(permitId) : new McPermit();
        McModule module = new McModule();
        if(NumberUtil.isNullOrZero(obj.getModuleId()) && NumberUtil.isNotNullOrZero(moduleId)) {
            obj.setModuleId(moduleId);
        }
        if(NumberUtil.isNotNullOrZero(obj.getModuleId())) {
            module = moduleService.findByKey(obj.getModuleId());
            obj.setSysId(module.getSysId());
        }
        McSys sys = new McSys();
        if(NumberUtil.isNotNullOrZero(module.getSysId())) {
            sys = sysService.findByKey(module.getSysId());
        }

        model.addAttribute("obj", obj);

        model.addAttribute("module", module);
        model.addAttribute("sys", sys);

        return "permit/detail";
    }
}
